//
//  UIViewController+PlaceHolder.m
//  LuxuryGoods
//
//  Created by 刘星辰 on 2018/5/19.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "UIViewController+PlaceHolder.h"
#import <objc/runtime.h>
#import "UIViewExt.h"
#import "UIPageDefines.h"
#import "LGColor.h"

@implementation UIViewController (PlaceHolder)
@dynamic contenView;
@dynamic contentImageView;
@dynamic messageLabel;
@dynamic reFreshButton;

- (void)hidePlaceHolder
{
    if (self.contenView)
    {
        self.contenView.hidden = YES;
    }
}

- (void)showNoContentViewWithHint:(NSString*)hint refreshButtonText:(NSString*)buttonText andRefreshButton:(SEL)action onView:(UIView*)view;
{
    [self setContentView];
    self.contenView.hidden = NO;
    if (action == nil) {
        self.reFreshButton.hidden = YES;
    }
    [self.reFreshButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [self.reFreshButton setTitle:buttonText forState:UIControlStateNormal];
    self.messageLabel.text = hint;
    [view addSubview:self.contenView];
    [view bringSubviewToFront:self.contenView];
    [self rebuildContentSubViewFrame];
}

- (void)rebuildContentSubViewFrame
{
    [self.messageLabel sizeToFit];
    
    self.contentImageView.size = self.contentImageView.image.size;
    
    self.contentImageView.left = (SCREEN_WIDTH - self.contentImageView.width) / 2.0;
    
    if (self.reFreshButton.hidden == YES)
    {
        self.contentImageView.bottom = (SCREEN_HEIGHT - self.contentImageView.height - 8 -self.messageLabel.height) / 2.0 - 40;
    }
    else
    {
        self.contentImageView.bottom = (SCREEN_HEIGHT - self.contentImageView.height - 8 -self.messageLabel.height - self.reFreshButton.height - 16) / 2.0 - 40;
    }
    
    self.messageLabel.top =  self.contentImageView.bottom + 8;
    
    self.messageLabel.left = (SCREEN_WIDTH - self.messageLabel.width) / 2.0;
    
    self.reFreshButton.top = self.messageLabel.bottom + 16;
    
    self.reFreshButton.left = (SCREEN_WIDTH - self.reFreshButton.width) / 2.0;
}

- (void)showNoContentViewWithHint:(NSString*)hint refreshButtonText:(NSString*)buttonText andRefreshButton:(SEL)action onView:(UIView*)view andPlaceHolderImageImage:(UIImage*)holderImage
{
    [self showNoContentViewWithHint:hint
                  refreshButtonText:buttonText
                   andRefreshButton:action
                             onView:view];
    self.contentImageView.image = holderImage;
    [self rebuildContentSubViewFrame];
}

- (void)showImageText:(NSString *)hint onView:(UIView *)view andPlaceHolderImage:(UIImage *)holderImage {
    [self referContenView];
    self.contenView.hidden = NO;
    self.messageLabel.text = hint;
    
    self.contentImageView.image = holderImage;
    [self rebuildContentSubViewFrame];
    [view addSubview:self.contenView];
    [view bringSubviewToFront:self.contenView];
    
    
}

- (UIView *)referContenView {
    if (!self.contenView) {
        self.contenView = [[UIView alloc] initWithFrame:self.view.bounds];
        self.contenView.backgroundColor = BACKGROUND_COLOR;
    }
    
    //图片
    self.contentImageView = [[UIImageView alloc]init];
    self.contentImageView.image  = [UIImage imageNamed:@"nocontent"];
    [self.contenView addSubview:self.contentImageView];
    
    //提示文字
    self.messageLabel = [[UILabel alloc]init];
    self.messageLabel.font = FONT(14);
    self.messageLabel.textColor = TEXT_COLOR_3;
    self.messageLabel.textAlignment = NSTextAlignmentCenter;
    self.messageLabel.numberOfLines = 0;
    [self.contenView addSubview:self.messageLabel];
    
    self.contentImageView.hidden = NO;
    self.messageLabel.hidden = NO;
    return self.contenView;
    
}


-(UIView*)setContentView
{
    if (!self.contenView)
    {
        self.contenView = [[UIView alloc]initWithFrame:self.view.bounds];
        self.contenView.backgroundColor = BACKGROUND_COLOR;
        
        //图片
        self.contentImageView = [[UIImageView alloc]init];
        self.contentImageView.contentMode = UIViewContentModeScaleAspectFit;
        self.contentImageView.image = [UIImage imageNamed:@"nocontent"];
        [self.contenView addSubview:self.contentImageView];
        
        //提示文字
        self.messageLabel = [[UILabel alloc]init];
        self.messageLabel.font = FONT(14);
        self.messageLabel.textColor = TEXT_COLOR_3;
        self.messageLabel.textAlignment = NSTextAlignmentCenter;
        self.messageLabel.numberOfLines = 0;
        [self.contenView addSubview:self.messageLabel];
        
        //刷新按钮
        self.reFreshButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.reFreshButton.size = CGSizeMake(170, 50);
        self.reFreshButton.backgroundColor = TOPCAIL_COLOR;
        [self.reFreshButton setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
        [self.reFreshButton setTitleColor:GREEN_SELECTED forState:UIControlStateHighlighted];
        [self.reFreshButton setTitle:@"重新加载" forState:UIControlStateNormal];
        self.reFreshButton.titleLabel.font = FONT(15);
        [self.contenView addSubview:self.reFreshButton];
    }
    self.contentImageView.hidden = NO;
    self.messageLabel.hidden = NO;
    self.reFreshButton.hidden = NO;
    return self.contenView;
}


//为新添加的属性添加 setter 和 getter 方法

//---contentImageView
static char contentImageViewKey;
-(void)setContentImageView:(UIImageView *)contentImageView
{
    objc_setAssociatedObject(self, &contentImageViewKey, contentImageView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(UIImageView*)contentImageView
{
    return objc_getAssociatedObject(self, &contentImageViewKey);
}

//---contenView
static char contenViewKey;
-(void)setContenView:(UIView *)contenView
{
    objc_setAssociatedObject(self, &contenViewKey, contenView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(UIView*)contenView
{
    return objc_getAssociatedObject(self, &contenViewKey);
}

//---messageLabel
static char messageLabelKey;
-(void)setMessageLabel:(UILabel *)messageLabel
{
    objc_setAssociatedObject(self, &messageLabelKey, messageLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(UILabel*)messageLabel
{
    return objc_getAssociatedObject(self, &messageLabelKey);
}

//---reFreshButton
static char reFreshButtonKey;
-(void)setReFreshButton:(UIButton *)reFreshButton
{
    objc_setAssociatedObject(self, &reFreshButtonKey, reFreshButton, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(UIButton*)reFreshButton
{
    return objc_getAssociatedObject(self, &reFreshButtonKey);
}

@end
